package com.runfast.waimai.util;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class MsgPushUtils {

    protected static Logger LOG = LoggerFactory.getLogger(MsgPushUtils.class);
    private static final String MASTER_SECRET2 = "4ac1486ca9dd2717207a50ee";
    private static final String APP_KEY2 = "165ccffde64bf5dd1ab7ff04";
    private static final String MASTER_SECRET = "2ab3f7deb57ab5703eb6d366";
    private static final String APP_KEY = "c94ba5202ef88b506181459f";
    private static PushPayload pushPayload;

    /**
     * 新版商家ISO
     */
    private static final String MASTER_SECRET3 = "f9ae4ce5095c7a1c3ec334d7";
    private static final String APP_KEY3 = "1354bc477364bc5f5271dda0";

    /**
     * 用户版
     */
    private static final String MASTER_SECRET_USER = "f01abcf874d90c586fdefe46";
    private static final String APP_KEY_USER = "caa0055eac2098fb55eaf77b";

    public static final String BEEN_CONCERNED_ALERT = "";

    public static final String SOUND = "";

    public static final int BADGE = 1;
    private static final boolean isApnsProduction = true;

    /**
     * @param @param  json
     * @param @return 设定文件
     * @return HttpResponse    返回类型
     * @throws APIRequestException
     * @throws APIConnectionException
     * @throws
     * @Title: pushMessage
     * @Description: (推送消息) 商家
     */
    public static PushResult sendPush(PushPayload payload) throws APIConnectionException, APIRequestException {
        JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        return jPushClient.sendPush(payload);
    }

    /**
     * 新版商家
     *
     * @param payload
     * @return
     * @throws APIConnectionException
     * @throws APIRequestException
     */
    public static PushResult sendPush2(PushPayload payload) throws APIConnectionException, APIRequestException {
        JPushClient jPushClient1 = new JPushClient(MASTER_SECRET3, APP_KEY3);
        return jPushClient1.sendPush(payload);
    }

    /**
     * @param @param  json
     * @param @return 设定文件
     * @return HttpResponse    返回类型
     * @throws APIRequestException
     * @throws APIConnectionException
     * @throws
     * @Title: pushMessage
     * @Description: (推送消息) 骑手
     */
    public static PushResult sendPush1(PushPayload payload) throws APIConnectionException, APIRequestException {
        JPushClient jPushClient = new JPushClient(MASTER_SECRET2, APP_KEY2);
        return jPushClient.sendPush(payload);
    }

    /**
     * @param @param  payload
     * @param @param  logger
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: sendPushTryCatch
     * @Description: try catch 推送
     */
    public static void sendPushTryCatch(PushPayload payload, Logger logger) {
        JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        try {
            PushResult pushResult = jPushClient.sendPush(payload);
            logger.info("返回结果" + pushResult);
        } catch (APIConnectionException e) {
            logger.error("连接错误，稍后尝试" + e);
        } catch (APIRequestException e) {
            logger.error("极光推送内部错误", e);
            logger.info("网络请求状态" + e.getStatus());
            logger.info("错误状态码" + e.getErrorCode());
            logger.info("错误信息" + e.getErrorMessage());
            logger.info("信息ID" + e.getMsgId());
            logger.info("极光推送错误信息:" + e.getErrorMessage());
        }
    }

    /**
     * @param @param  alert
     * @param @return 设定文件
     * @return PushPayload    返回类型
     * @throws
     * @Title: buildPushObjectAllAllAlert
     * @Description: (快捷地构建推送对象：所有平台，所有设备，内容为 alert 的通知)
     */
    @SuppressWarnings("static-access")
    public static PushPayload buildPushObjectAllAllAlert(String alert) {
        return pushPayload.alertAll(alert);
    }

    /**
     * @param @param  alert
     * @param @param  alias
     * @param @return 设定文件
     * @return PushPayload    返回类型
     * @throws
     * @Title: buildPushObjectAliasAlert
     * @Description: (所有平台，推送目标是别名为 alias，通知内容为 alert)
     */
    public static PushPayload buildPushObjectAliasAlert(String days, String alert, String... alias) {
        PushPayload.Builder builder = PushPayload.newBuilder();
        return builder
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .setAlert(alert)
                        .addPlatformNotification(
                                AndroidNotification.newBuilder()
                                        .addExtra("sign", "5")
                                        .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .addExtra("sign", "5")
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(isApnsProduction)
                        .setTimeToLive(Long.valueOf(Integer.valueOf(days) * 86400))
                        .build())
                .build();
    }

    /**
     * 商家IOS
     *
     * @param days
     * @param tag
     * @param tagAll
     * @param number
     * @param sound
     * @param alert
     * @param msgContent
     * @return
     */
    public static int sendtoIOS1(String days,
                                 String tag, String tagAll, int number, String sound, String alert, String msgContent) {
        int result = 0;
        if (tag != null && tagAll != null) {
            if (!tag.isEmpty() && !tagAll.isEmpty()) {
                String[] split = tag.split(",", 0);
                for (String s : split) {
                    if(StringUtils.isNotBlank(s)){
                        String[] strings = s.split(";", 0);
                        if(strings.length==3){
                            String alias = strings[0];
                            String bptype = strings[1];
                            String pushtype = strings[2];
                            if(bptype.equals("0")&&pushtype.equals("3")){//安卓用户
                                Map<String,String> parms = new HashMap<>();
                            /*parms.put("Id", goodsSellRecord.getBusinessId().toString());
                            //	     parms.put("Content", "您的订单:"+goodsSellRecord.getOrderCode()+"客户申请取消订单");
                            //	     parms.put("title", "取消订单申请提醒");
                            parms.put("bptype", ba.getBptype().toString());
                            parms.put("sound","newcancel.caf");*/

                                MsgPushUtils.sendtoAndroidBusiness("10",parms,"取消订单申请提醒", "客户申请取消订单", alias);//发送给安卓iod特定用户


                            }else if(bptype.equals("1")&&pushtype.equals("3")){

                                MsgPushUtils.sendtoIOSBusiness("10", alias,alias, 1, "newcancel.caf", "取消订单申请提醒", "取消订单申请提醒");

                            }else if(bptype.equals("1")&&pushtype.equals("4")){

                                MsgPushUtils.sendtoIOSBusiness2("10", alias,alias, 1, "newcancel.caf", "取消订单申请提醒", "取消订单申请提醒");

                            }
                        }

                    }
                }

            }
        }
        return result;
    }

    /**
     * 商家ios
     * @param days
     * @param tag
     * @param tagAll
     * @param number
     * @param sound
     * @param alert
     * @param msgContent
     * @return
     */
    public static int sendtoIOSBusiness(String days,
                                        String tag, String tagAll, int number, String sound, String alert, String msgContent) {
        int result = 0;
        if (tag != null && tagAll != null) {
            if (!tag.isEmpty() && !tagAll.isEmpty()) {
                try {
                    PushPayload pushPayload = MsgPushUtils.buildPushObjectIosTagAndAlertWithExtrasAndMessage("10", tag, tagAll, number, sound, alert, msgContent);
                    PushResult pushResult = MsgPushUtils.sendPush(pushPayload);
                    System.out.println(pushResult);
                    if (pushResult.getResponseCode() == 200) {
                        result = 1;
                    }
                } catch (APIConnectionException e) {
                    e.printStackTrace();

                } catch (APIRequestException e) {
                    e.printStackTrace();
                }

            }
        }
        return result;
    }


    /**
     * 新版商家IOS
     *
     * @param days
     * @param tag
     * @param tagAll
     * @param number
     * @param sound
     * @param alert
     * @param msgContent
     * @return
     */
    public static int sendtoIOS2(String days,
                                 String tag, String tagAll, int number, String sound, String alert, String msgContent) {
        int result = 0;
        if (tag != null && tagAll != null) {
            if (!tag.isEmpty() && !tagAll.isEmpty()) {
                String[] split = tag.split(",", 0);
                for (String s : split) {
                    if(StringUtils.isNotBlank(s)){
                        String[] strings = s.split(";", 0);
                        if(strings.length==3){
                            String alias = strings[0];
                            String bptype = strings[1];
                            String pushtype = strings[2];
                            if(bptype.equals("0")&&pushtype.equals("3")){//安卓用户
                                Map<String,String> parms = new HashMap<>();
                            /*parms.put("Id", goodsSellRecord.getBusinessId().toString());
                            //	     parms.put("Content", "您的订单:"+goodsSellRecord.getOrderCode()+"客户申请取消订单");
                            //	     parms.put("title", "取消订单申请提醒");
                            parms.put("bptype", ba.getBptype().toString());
                            parms.put("sound","newcancel.caf");*/

                                MsgPushUtils.sendtoAndroidBusiness("10",parms,"取消订单申请提醒", "客户申请取消订单", alias);//发送给安卓iod特定用户


                            }else if(bptype.equals("1")&&pushtype.equals("3")){

                                MsgPushUtils.sendtoIOSBusiness("10", alias,alias, 1, "newcancel.caf", "取消订单申请提醒", "取消订单申请提醒");

                            }else if(bptype.equals("1")&&pushtype.equals("4")){

                                MsgPushUtils.sendtoIOSBusiness2("10", alias,alias, 1, "newcancel.caf", "取消订单申请提醒", "取消订单申请提醒");

                            }
                        }

                    }
                }

            }
        }
        return result;
    }

    /**
     * 新版商家ios
     * @param days
     * @param tag
     * @param tagAll
     * @param number
     * @param sound
     * @param alert
     * @param msgContent
     * @return
     */
    public static int sendtoIOSBusiness2(String days,
                                         String tag, String tagAll, int number, String sound, String alert, String msgContent) {
        int result = 0;
        if (tag != null && tagAll != null) {
            if (!tag.isEmpty() && !tagAll.isEmpty()) {

                try {
                    PushPayload pushPayload = MsgPushUtils.buildPushObjectIosTagAndAlertWithExtrasAndMessage("10", tag, tagAll, number, sound, alert, msgContent);

                    PushResult pushResult = MsgPushUtils.sendPush2(pushPayload);
                    System.out.println(pushResult);
                    if (pushResult.getResponseCode() == 200) {
                        result = 1;
                    }
                } catch (APIConnectionException e) {
                    e.printStackTrace();

                } catch (APIRequestException e) {
                    e.printStackTrace();
                }

            }
        }
        return result;
    }

    /**
     * 商家 安卓推送
     *
     * @param days
     * @param extras
     * @param title
     * @param msgContent
     * @param alias
     * @return
     */
    public static int sendtoAndroid1(String days, Map<String, String> extras, String title, String msgContent, String alias) {
        int result = 0;
        if (alias != null) {
            if (!alias.isEmpty()) {
                String[] split = alias.split(",", 0);
                for (String s : split) {
                    if(StringUtils.isNotBlank(s)){
                        alias = s;// strings[0];
                        String bptype = "";//strings[1];
                        String pushtype = "";//strings[2];
                        if(!alias.isEmpty())
                            {
                                //9A4335F24D9F4DC7ABC5E89CE8CB3FE8;1;4,89739FB3341F45FFAF89B9B3CC3DB1B8;1;4,860241032561101;0;3,865982020528058;0;3,864181027369381;0;3
                                if(alias.length() > 20 ) { //ios
                                    bptype = "1";
                                    pushtype = "4";
                                }
                                else{//安卓
                                    bptype = "0";
                                    pushtype = "3";
                                }
                            }
                            if(bptype.equals("0")&&pushtype.equals("3")){//安卓用户

                                if(extras.get("sound").contains("neworder")) {
                                    MsgPushUtils.sendtoAndroidBusiness("10",extras,"新订单提醒", "您有新订单", alias);//发送给安卓iod特定用户
                                }else{
                                    MsgPushUtils.sendtoAndroidBusiness("10",extras,"取消订单申请提醒", "取消订单申请提醒", alias);
                                }
                            }else if(bptype.equals("1")&&pushtype.equals("3")){
                                if(extras.get("sound").contains("neworder")) {
                                    MsgPushUtils.sendtoIOSBusiness("10", alias, alias, 1, "neworder.caf", "新订单提醒", "您有新订单");
                                }else{
                                    MsgPushUtils.sendtoIOSBusiness("10", alias, alias, 1,  "newcancel.caf", "取消订单申请提醒", "取消订单申请提醒");
                                }

                            }else if(bptype.equals("1")&&pushtype.equals("4")){
                                if(extras.get("sound").contains("neworder")) {
                                    MsgPushUtils.sendtoIOSBusiness2("10", alias,alias, 1,"neworder.caf", "新订单提醒", "您有新订单");
                                }else{
                                    MsgPushUtils.sendtoIOSBusiness2("10", alias,alias, 1, "newcancel.caf", "取消订单申请提醒", "取消订单申请提醒");
                                }


                            }
                        }

                    }

            }
        }
        return result;
    }

    /**
     * 商家安卓
     * @param days
     * @param extras
     * @param title
     * @param msgContent
     * @param alias
     * @return
     */
    private static int sendtoAndroidBusiness(String days, Map<String, String> extras, String title, String msgContent, String alias) {
        int result = 0;
        if (alias != null) {
            if (!alias.isEmpty()) {

                try {
                    PushPayload pushPayload = MsgPushUtils.sendAndroidAndIosMessageWithAlias("10", extras, title, msgContent, alias);
                    PushResult pushResult = MsgPushUtils.sendPush(pushPayload);
                    System.out.println(pushResult);
                    if (pushResult.getResponseCode() == 200) {
                        result = 1;
                    }
                } catch (APIConnectionException e) {
                    e.printStackTrace();

                } catch (APIRequestException e) {
                    e.printStackTrace();
                }

            }
        }
        return result;
    }

    /**
     * 骑手IOS
     *
     * @param days
     * @param tag
     * @param tagAll
     * @param number
     * @param sound
     * @param alert
     * @param msgContent
     * @return
     */
    public static int sendtoIOS(String days,
                                String tag, String tagAll, int number, String sound, String alert, String msgContent) {
        int result = 0;
        if (tag != null && tagAll != null) {
            if (!tag.isEmpty() && !tagAll.isEmpty()) {
                try {
                    PushPayload pushPayload = MsgPushUtils.buildPushObjectIosTagAndAlertWithExtrasAndMessage("10", tag, tagAll, number, sound, alert, msgContent);
                    PushResult pushResult = MsgPushUtils.sendPush1(pushPayload);
                    System.out.println(pushResult);
                    if (pushResult.getResponseCode() == 200) {
                        result = 1;
                    }
                } catch (APIConnectionException e) {
                    e.printStackTrace();

                } catch (APIRequestException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 骑手 安卓推送
     *
     * @param days
     * @param extras
     * @param title
     * @param msgContent
     * @param alias
     * @return
     */
    public static int sendtoAndroid(String days, Map<String, String> extras, String title, String msgContent, String alias) {
        int result = 0;
        if (alias != null) {
            if (!alias.isEmpty()) {
                try {
                    PushPayload pushPayload = MsgPushUtils.sendAndroidAndIosMessageWithAlias("10", extras, title, msgContent, alias);
                    PushResult pushResult = MsgPushUtils.sendPush1(pushPayload);
                    System.out.println(pushResult);
                    if (pushResult.getResponseCode() == 200) {
                        result = 1;
                    }
                } catch (APIConnectionException e) {
                    e.printStackTrace();

                } catch (APIRequestException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     *
     */
    public static int sendto(String days,
                             String tag, String tagAll, int number, String sound, String alert, String msgContent) {
        int result = 0;
        try {
            PushPayload pushPayload = MsgPushUtils.buildPushObjectIosTagAndAlertWithExtrasAndMessage(days, tag, tagAll, number, sound, alert, msgContent);
            PushResult pushResult = MsgPushUtils.sendPush1(pushPayload);
            System.out.println(pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();

        } catch (APIRequestException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 推送给设备标识参数的用户
     *
     * @param registrationId     设备标识
     * @param notification_title 通知内容标题
     * @param msg_title          消息内容标题
     * @param msg_content        消息内容
     * @param extrasparam        扩展字段
     * @return 0推送失败，1推送成功
     */
    public static int sendToRegistrationId(String registrationId, String notification_title, String msg_title, String msg_content, String extrasparam) {
        int result = 0;
        try {
            PushPayload pushPayload = MsgPushUtils.buildPushObject_all_registrationId_alertWithTitle(registrationId, notification_title, msg_title, msg_content, extrasparam);
            System.out.println(pushPayload);
            PushResult pushResult = MsgPushUtils.sendPush1(pushPayload);
            System.out.println(pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();

        } catch (APIRequestException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * @param @param  alias
     * @param @param  alert
     * @param @param  badge
     * @param @return 设定文件
     * @return PushPayload    返回类型
     * @throws
     * @Title: buildPushObjectIos
     * @Description: (iOS推送 badge sound)
     */
    private static PushPayload buildPushObject_all_registrationId_alertWithTitle(String registrationId, String notification_title, String msg_title, String msg_content, String extrasparam) {

        System.out.println("----------buildPushObject_all_all_alert");
        //创建一个IosAlert对象，可指定APNs的alert、title等字段
        //IosAlert iosAlert =  IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();

        return PushPayload.newBuilder()
                //指定要推送的平台，all代表当前应用配置了的所有平台，也可以传android等具体平台
                .setPlatform(Platform.all())
                //指定推送的接收对象，all代表所有人，也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
                .setAudience(Audience.registrationId(registrationId))
                //jpush的通知，android的由jpush直接下发，iOS的由apns服务器下发，Winphone的由mpns下发
                .setNotification(Notification.newBuilder()
                        //指定当前推送的android通知
                        .addPlatformNotification(AndroidNotification.newBuilder()

                                .setAlert(notification_title)
                                .setTitle(notification_title)
                                //此字段为透传字段，不会显示在通知栏。用户可以通过此字段来做一些定制需求，如特定的key传要指定跳转的页面（value）
                                .addExtra("androidNotification extras key", extrasparam)

                                .build())
                        //指定当前推送的iOS通知
                        .addPlatformNotification(IosNotification.newBuilder()
                                //传一个IosAlert对象，指定apns title、title、subtitle等
                                .setAlert(notification_title)
                                //直接传alert
                                //此项是指定此推送的badge自动加1
                                .incrBadge(1)
                                //此字段的值default表示系统默认声音；传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒，
                                // 如果系统没有此音频则以系统默认声音提醒；此字段如果传空字符串，iOS9及以上的系统是无声音提醒，以下的系统是默认声音
                                .setSound("sound.caf")
                                //此字段为透传字段，不会显示在通知栏。用户可以通过此字段来做一些定制需求，如特定的key传要指定跳转的页面（value）
                                .addExtra("iosNotification extras key", extrasparam)
                                //此项说明此推送是一个background推送，想了解background看：http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                //取消此注释，消息推送时ios将无法在锁屏情况接收
                                // .setContentAvailable(true)

                                .build())


                        .build())
                //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息，
                // sdk默认不做任何处理，不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
                // [通知与自定义消息有什么区别？]了解通知和自定义消息的区别
                .setMessage(Message.newBuilder()

                        .setMsgContent(msg_content)

                        .setTitle(msg_title)

                        .addExtra("message extras key", extrasparam)

                        .build())

                .setOptions(Options.newBuilder()
                        //此字段的值是用来指定本推送要推送的apns环境，false表示开发，true表示生产；对android和自定义消息无意义
                        .setApnsProduction(isApnsProduction)
                        //此字段是给开发者自己给推送编号，方便推送者分辨推送记录
                        .setSendno(1)
                        //此字段的值是用来指定本推送的离线保存时长，如果不传此字段则默认保存一天，最多指定保留十天；
                        .setTimeToLive(86400)

                        .build())

                .build();

    }

    public static PushPayload buildPushObjectIosAndroid(String days, Map<String, String> params,
                                                        String[] alias, String alert, int badge, String sound, String msgContent) {
        PushPayload.Builder builder = PushPayload.newBuilder();
        return builder
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .setBadge(badge)
                                .addExtras(params)
                                .setSound(sound)
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(alert)
                                .addExtras(params)
                                .build())
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(msgContent)
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(isApnsProduction)
                        .setTimeToLive(Long.valueOf(Integer.valueOf(days) * 86400))
                        .build())
                .build();
    }

    /**
     * @param @param  params
     * @param @param  alias
     * @param @return 设定文件
     * @return PushPayload    返回类型
     * @throws
     * @Title: buildPushObjectAllAliasAlert
     * @Description: (所有平台，推送目标是别名为 alias，通知标题为 title，通知内容为 alert)
     */
    public static PushPayload buildPushObjectAllAliasAlert(String days, Map<String, String> params, String alert, String title, String... alias) {
        PushPayload.Builder builder = PushPayload.newBuilder();
        return builder
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .setAlert(alert)
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(title)
                                .addExtras(params)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .addExtras(params)
                                .setBadge(1)
                                .build())
                        .build())
                .setMessage(Message.content(alert))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(isApnsProduction)
                        .setTimeToLive(Long.valueOf(Integer.valueOf(days) * 60))
                        .build())
                .build();
    }


    /**
     * @param @param  tag
     * @param @param  alert
     * @param @param  title
     * @param @return 设定文件
     * @return PushPayload    返回类型
     * @throws
     * @Title: buildPushObjectAndroidTagAlertWithTitle
     * @Description: (平台是 Android，目标是 tag 为 tag 的设备，内容是 Android 通知 alert，并且标题为 title)
     */
    public static PushPayload buildPushObjectAndroidTagAlertWithTitle(String days, String tag, String alert, String title) {
        PushPayload.Builder builder = PushPayload.newBuilder();
        return builder
                .setPlatform(Platform.android())
                .setAudience(Audience.tag(tag))
                .setNotification(Notification.android(alert, title, null))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(isApnsProduction)
                        .setTimeToLive(Long.valueOf(Integer.valueOf(days) * 86400))
                        .build())
                .build();
    }

    /**
     * @param @param  tag
     * @param @param  tagAll
     * @param @param  number
     * @param @param  alert
     * @param @param  msgContent
     * @param @return 设定文件
     * @return PushPayload    返回类型
     * @throws
     * @Title: buildPushObjectIosTagAndAlertWithExtrasAndMessage
     * @Description: (
     *构建推送对象：平台是 iOS，推送目标是 tag, tagAll 的交集，
     * 推送内容同时包括通知与消息 - 通知信息是 alert，角标数字为 number，
     * 消息内容是 msgContent。
     * 通知是 APNs 推送通道的，消息是 JPush 应用内消息通道的。
     * APNs 的推送环境是“开发”（如果不显式设置的话，Library 会默认指定为开发）
     * true 表示推送生产环境，true 表示要推送开发环境
     *)
     */
    public static PushPayload buildPushObjectIosTagAndAlertWithExtrasAndMessage(String days,
                                                                                String tag, String tagAll, int number, String sound, String alert, String msgContent) {
        PushPayload.Builder builder = PushPayload.newBuilder();
        return builder
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and(tag, tagAll))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .setBadge(number)
                                .setSound(sound)
                                .build())
                        .build())
                .setMessage(Message.content(msgContent))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(isApnsProduction)
//                             .setTimeToLive(Long.valueOf(Integer.valueOf(days)*86400))
                        .setTimeToLive(Long.valueOf(Integer.valueOf(days) * 60))
                        .build())
                .build();
    }

    /**
     * 构建推送对象：平台是 Andorid 与 iOS，
     * 推送目标是 （tag1 与 tag2 的并集），
     * 推送内容是 - 内容为 msgContent 的消息
     *
     * @param @param  tag1
     * @param @param  tag2
     * @param @param  msgContent
     * @param @return 设定文件
     * @return PushPayload    返回类型
     * @throws
     * @Title: buildPushObjectIosAudienceMoreMessageWithExtras
     */
    public static PushPayload buildPushObjectIosAudienceMoreMessageWithExtras(String days,
                                                                              String tag1, String tag2, String msgContent) {
        PushPayload.Builder builder = PushPayload.newBuilder();
        return builder
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag(tag1, tag2))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(msgContent)
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(isApnsProduction)
                        .setTimeToLive(Long.valueOf(Integer.valueOf(days) * 86400))
                        .build())
                .build();
    }

    /**
     * @param @param  extras
     * @param @param  title
     * @param @param  msgContent
     * @param @param  alias
     * @param @return
     * @param @throws APIConnectionException
     * @param @throws APIRequestException    设定文件
     * @return PushPayload    返回类型
     * @throws
     * @Title: sendAndroidMessageWithAliasAndExtras
     * @Description: 用户自定义消息类型, 以及传递附属信息, 发送给安卓和IOS特定用户
     */
    public static PushPayload sendAndroidAndIosMessageWithAliasAndExtras(String days, Map<String, String> extras, String title, String msgContent, String... alias)
            throws APIConnectionException, APIRequestException {
        PushPayload.Builder builder = PushPayload.newBuilder();
        return builder
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setMessage(Message.newBuilder()
                        .setTitle(title)
                        .setMsgContent(msgContent)
                        .addExtras(extras)
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(isApnsProduction)
                        .setTimeToLive(Long.valueOf(Integer.valueOf(days) * 86400))
                        .build())
                .build();
    }

    /**
     * @param @param  title
     * @param @param  msgContent
     * @param @param  alias
     * @param @return
     * @param @throws APIConnectionException
     * @param @throws APIRequestException    设定文件
     * @return PushPayload    返回类型
     * @throws
     * @Title: sendAndroidMessageWithAlias
     * @Description: 用户自定义消息类型，无附加属性信息，发送给安卓和IOS特定用户
     */
    public static PushPayload sendAndroidAndIosMessageWithAlias(String days, Map<String, String> extras, String title, String msgContent, String... alias)
            throws APIConnectionException, APIRequestException {
        PushPayload.Builder builder = PushPayload.newBuilder();
        return builder
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setMessage(Message.newBuilder()
                        .setTitle(title)
                        .setMsgContent(msgContent)
                        .addExtras(extras)
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(isApnsProduction)
                        .setTimeToLive(Long.valueOf(Integer.valueOf(days) * 60))
                        .build())
                .build();
    }

    /**
     * @param @param  title
     * @param @param  alert
     * @param @return 设定文件
     * @return PushPayload    返回类型
     * @throws
     * @Title: sendAndroidAndIosMessageAndNotification
     * @Description: 推送消息和通知，发送给所有安卓和IOS用户
     */
    public static PushPayload sendAndroidAndIosMessageAndNotification(String days, String content, String alert) {
        PushPayload.Builder builder = PushPayload.newBuilder();
        return builder
                .setPlatform(Platform.android_ios())
                .setNotification(Notification.newBuilder()
                        .setAlert(alert)
                        .build())
                .setMessage(Message.newBuilder()
                        .setTitle("")
                        .setMsgContent(content)
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(isApnsProduction)
                        .setTimeToLive(Long.valueOf(Integer.valueOf(days) * 86400))
                        .build())
                .build();
    }

//      public static void main(String[] args) {
//          try {
//              sendPush(sendAndroidAndIosMessageWithAlias("1", null,"超神了哦", "123456"));
//          } catch (APIConnectionException e) {
//              LOG.error("Connection error. Should retry later. ", e);
//          } catch (APIRequestException e) {
//              LOG.error("Error response from JPush server. Should review and fix it. ", e);
//              LOG.info("HTTP Status: " + e.getStatus());
//              LOG.info("Error Code: " + e.getErrorCode());
//              LOG.info("Error Message: " + e.getErrorMessage());
//          }
//
//      }


    public static PushResult pushToUser(Integer timeout, HashMap<String, String> params, String title, String content, String alias){

        if(StringUtils.isBlank(alias))return null;
        JPushClient jpushClient = new JPushClient(MASTER_SECRET_USER, APP_KEY_USER);

        // For push, all you need do is to build PushPayload object.
        PushPayload payload = buildPushObjectAllAliasAlert(timeout+"", params, content, title, alias);

        try {
            PushResult result = jpushClient.sendPush(payload);
            LOG.info("Got result - " + result);
            return result;

        } catch (APIConnectionException e) {
            // Connection error, should retry later
            LOG.error("Connection error, should retry later", e);

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            LOG.error("Should review the error, and fix the request", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
        }
        return null;
    }
    //********************************************************
    public static int sendtoAndroid1Test(String days, Map<String, String> extras, String title, String msgContent, String alias) {
        int result = 0;
        if (alias != null) {
            if (!alias.isEmpty()) {
                String[] split = alias.split(",", 0);
                for (String s : split) {
                    if(StringUtils.isNotBlank(s)){
                        alias = s;// strings[0];
                        String bptype = "";//strings[1];
                        String pushtype = "";//strings[2];
                        if(!alias.isEmpty())
                        {
                            //9A4335F24D9F4DC7ABC5E89CE8CB3FE8;1;4,89739FB3341F45FFAF89B9B3CC3DB1B8;1;4,860241032561101;0;3,865982020528058;0;3,864181027369381;0;3
                            if(alias.length() > 20 ) { //ios
                                bptype = "1";
                                pushtype = "4";
                            }
                            else{//安卓
                                bptype = "0";
                                pushtype = "3";
                            }
                        }
                        MsgPushUtils.sendtoAndroidBusiness("10",extras,"新订单提醒", "您有新订单", alias);//发送给安卓iod特定用户
//                        if(bptype.equals("0")&&pushtype.equals("3")){
//                            //安卓用户
//
//                            if(extras.get("sound").contains("neworder")) {
//                                MsgPushUtils.sendtoAndroidBusiness("10",extras,"新订单提醒", "您有新订单", alias);//发送给安卓iod特定用户
//                            }else{
//                                MsgPushUtils.sendtoAndroidBusiness("10",extras,"取消订单申请提醒", "取消订单申请提醒", alias);
//                            }
//                        }else if(bptype.equals("1")&&pushtype.equals("3")){
//                            if(extras.get("sound").contains("neworder")) {
//                                MsgPushUtils.sendtoIOSBusiness("10", alias, alias, 1, "neworder.caf", "新订单提醒", "您有新订单");
//                            }else{
//                                MsgPushUtils.sendtoIOSBusiness("10", alias, alias, 1,  "newcancel.caf", "取消订单申请提醒", "取消订单申请提醒");
//                            }
//
//                        }else if(bptype.equals("1")&&pushtype.equals("4")){
//                            if(extras.get("sound").contains("neworder")) {
//                                MsgPushUtils.sendtoIOSBusiness2("10", alias,alias, 1,"neworder.caf", "新订单提醒", "您有新订单");
//                            }else{
//                                MsgPushUtils.sendtoIOSBusiness2("10", alias,alias, 1, "newcancel.caf", "取消订单申请提醒", "取消订单申请提醒");
//                            }
//
//
//                        }
                    }

                }

            }
        }
        return result;
    }
}
